package com.api.user.service.impl;

import com.api.common.mybatis.ResultMap;
import com.api.user.dao.AchievementApplyMapper;
import com.api.user.dao.AchievementMapper;
import com.api.user.model.Achievement;
import com.api.user.model.AchievementApply;
import com.api.user.model.PointsTransaction;
import com.api.user.service.AchievementService;
import com.api.core.service.AbstractService;
import com.api.user.service.PointsTransactionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.api.common.JSONUtils;
import com.api.core.response.Result;
import com.api.core.response.ResultGenerator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * Created by wanghuiwen on 2021/08/10.
 */
@Service
@Transactional
public class AchievementServiceImpl extends AbstractService<Achievement> implements AchievementService {
    @Resource
    private AchievementMapper achievementMapper;
    @Resource
    private AchievementApplyMapper achievementApplyMapper;
    @Resource
    private PointsTransactionService pointsTransactionService;

    @Override
    public Result list(String search, String order, Integer page, Integer size){
        Map<String, Object> params = JSONUtils.json2map(search);
        Map<String, Object> orderParams = JSONUtils.json2map(order);
        for (String key : orderParams.keySet()) {
                if (orderParams.get(key) != null && orderParams.get(key).equals("ascending")) orderParams.put(key, "asc");
                if (orderParams.get(key) != null && orderParams.get(key).equals("descending")) orderParams.put(key, "desc");
            }
        PageHelper.startPage(page, size);
        List<Map<String, Object>> res = achievementMapper.baseList(params, orderParams);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(res);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @Override
    public Result all(Integer page, Integer size, Long id) {
        List<ResultMap<String,Object>> achievements =  achievementMapper.all(id);
        return ResultGenerator.genSuccessResult(achievements);
    }

    @Override
    public Result my(Integer page, Integer size, Long id) {
        List<ResultMap<String,Object>> achievements =  achievementMapper.my(id);
        return ResultGenerator.genSuccessResult(achievements);
    }

    @Override
    public Result complete(Long achievementId,Long userId) {

        Achievement achievement = findById(achievementId);
        AchievementApply  oachievementApply = achievementApplyMapper.findByUser(achievementId,userId,null);

        if(oachievementApply.getState().equals("2")) {
            return ResultGenerator.genFailResult("You have get the badge");
        }
        oachievementApply.setAgreeTime(new Date().getTime());
        oachievementApply.setState("2");
        achievementApplyMapper.updateByPrimaryKeySelective(oachievementApply);

        PointsTransaction pointsTransaction = new PointsTransaction();
        pointsTransaction.setToUserId(userId);
        pointsTransaction.setOrderId(oachievementApply.getAchievementApplyId());
        pointsTransaction.setPointAmount(achievement.getBonus());
        pointsTransaction.setTransactionTime(new Date().getTime());
        pointsTransaction.setTransactionType(2);
        pointsTransaction.setCreateId(userId);
        pointsTransactionService.addOrSub(pointsTransaction);

        return ResultGenerator.genSuccessResult();
    }

    @Override
    public Result addStep(Long achievementId, Long userId, int step) {
        Achievement achievement = findById(achievementId);
        AchievementApply  oachievementApply = achievementApplyMapper.findByUser(achievementId,userId,null);

        if(oachievementApply==null){
            oachievementApply = new AchievementApply();
            oachievementApply.setAchievementId(achievementId);
            oachievementApply.setCreateTime(new Date().getTime());
            oachievementApply.setUserId(userId);
            oachievementApply.setState("1");
            oachievementApply.setStep(step);
            achievementApplyMapper.insertSelective(oachievementApply);
            if(step >= achievement.getTarget()){
                complete(achievementId,userId);
            }
            return ResultGenerator.genSuccessResult();
        }

        if(oachievementApply.getState().equals("2")) {
            return ResultGenerator.genFailResult("You have get the badge");
        }

        if(achievement.getTarget() > oachievementApply.getStep()){
            oachievementApply.setStep(step);
            achievementApplyMapper.updateByPrimaryKeySelective(oachievementApply);
            if(achievement.getTarget() <= oachievementApply.getStep()){
                complete(achievementId,userId);
            }
        }
        return ResultGenerator.genSuccessResult();
    }

    @Override
    public Result cleaning() {
        List<AchievementApply> achievementApplies = achievementApplyMapper.overdue(7);
        for (AchievementApply achievementApply : achievementApplies) {
            achievementApply.setStep(0);
            achievementApply.setAgreeTime(null);
            achievementApply.setState("1");
            achievementApplyMapper.updateByPrimaryKey(achievementApply);
        }
        return ResultGenerator.genSuccessResult();
    }
}
